//# sourceURL=userDetail.js
layui.element.on('tab(login-user-tabs)', function (layui_obj) {
    var custom_obj = $('#tabTitles')[0].children[layui_obj.index].tabCustomObj;
    var old_user_roles, old_phone;
    var userDetail_fn = {
        initRender: function () {
            //设置动态tab页内容主体的高度
            cmsUtils.setContentTabHeight('userDetailContent', 50);
            $('#userDetail').attr('hasRender', true);
            if (custom_obj.id) {
                var url = '/cms/sysManage/user/info/'+custom_obj.id;
                $.get(url, function (data) {
                    old_user_roles = data.data.user_roles;
                    old_phone = data.data.user_info.phone;
                    // 加载部门关联角色
                    $.get('/cms/sysManage/getRoles', function (data) {
                        $('#userDetail-div-roles').empty();
                        $.each(data.data, function (index, item) {
                            $('#userDetail-div-roles').append(
                                '<input type="checkbox" name="roleId['+item.id+']" title="'+item.name+'" value="'+item.id+'">'
                            );
                        });
                        // 设置已选角色
                        $.each(old_user_roles, function (index, item) {
                            var checkboxElem = $('#userDetail-div-roles').find('input[name="roleId['+item.id+']"]')[0];
                            if (checkboxElem) {
                                checkboxElem.checked = true;
                            }
                        });
                        layui.form.render(null, 'userDetail-form');
                    });
                    console.log(data);
                    userDetail_fn.setFormData(data);
                    $('button[lay-filter="userDetail-btn-modify"]').show();
                    $('#userDetail-div-phone').show();
                    $('#userDetail-input-phone').attr('lay-verify', 'required|phone');
                    $('#userDetail-input-phone').attr('lay-verType', 'tips');
                    $('#userDetail-div-username').hide();
                    $('#userDetail-input-username').attr('lay-verify', null);
                    $('#userDetail-input-username').attr('lay-verType', null);
                    $('#userDetail-div-createDate').show();
                    $('#userDetail-div-lastLogin').show();
                });
            } else {
                console.log(1111);
                $('button[lay-filter="userDetail-btn-add-continue"]').show();
                $('button[lay-filter="userDetail-btn-add"]').show();
                $('#userDetail-div-phone').hide();
                $('#userDetail-input-phone').attr('lay-verify', null);
                $('#userDetail-input-phone').attr('lay-verType', null);
                $('#userDetail-div-username').show();
                $('#userDetail-input-username').attr('lay-verify', 'required|phone');
                $('#userDetail-input-username').attr('lay-verType', 'tips');
                $.get('/cms/sysManage/getRoles', function (data) {
                    $('#userDetail-div-roles').empty();
                    $.each(data.data, function (index, item) {
                        $('#userDetail-div-roles').append(
                            '<input type="checkbox" name="roleId['+item.id+']" title="'+item.name+'" value="'+item.id+'">'
                        );
                    });
                    layui.form.render(null, 'userDetail-form');
                });
            }
            layui.form.render(null, 'userDetail-form');
        },
        setFormData: function (data) {
            cmsUtils.formDataBind('userDetail-form', data.data.user_info);
            $('#userDetail-div-roles').find(':checkbox').each(function (elem_index, elem_item) {
                $.each(data.data.user_roles, function (index, item) {
                    if (elem_item.value == item.id) {
                        elem_item.checked = true;
                    }
                });
            });
            layui.form.render(null, 'userDetail-form');
        },
        getFormData: function (data) {
            var user_roles_del = '';
            if (old_user_roles) {
                $.each(old_user_roles, function (old_index, old_item) {
                    var isDel = true;
                    for (var key in data) {
                        if (key.indexOf('roleId') != -1) {
                            if (data[key] == old_item.id) {
                                isDel = false;
                            }
                        }
                    }
                    if (isDel) {
                        user_roles_del += user_roles_del ? (','+old_item.id) : old_item.id;
                    }
                });
            }
            var user_roles_add = '';
            for (var key in data) {
                if (key.indexOf('roleId') != -1) {
                    var isAdd = true;
                    if (old_user_roles) {
                        $.each(old_user_roles, function (old_index, old_item) {
                            if (data[key] == old_item.id) {
                                isAdd = false;
                            }
                        });
                    }
                    if (isAdd) {
                        user_roles_add += user_roles_add ? (','+data[key]) : data[key];
                    }
                }
            }
            for (var key in data) {
                if (key.indexOf('roleId') != -1) {
                    delete data[key];
                }
            }
            data.id = custom_obj.id ? custom_obj.id : 0;
            data['user_roles_add'] = user_roles_add;
            data['user_roles_del'] = user_roles_del;
            return data;
        },
        verifyForm: function () {
            if ($('#userDetail-div-roles').find('input[type=checkbox]:checked').length <= 0) {
                layer.msg('请选择关联角色！', {anim: 6});
                return false;
            }
            return true;
        },
        submitForm: function (post_params, action, index) {
            var post_url = custom_obj.id ? '/cms/sysManage/editUser' : '/cms/sysManage/addUser';
            $.ajax({
                type: 'post',
                url: post_url,
                data: post_params,
                dataType: 'json',
                success: function (data) {
                    layer.close(index);
                    if (data.code == 200) {
                        switch (action) {
                            case 'continue':
                                $('#userDetail-form')[0].reset();
                                break;
                            case 'close':
                                layui.element.tabDelete('login-user-tabs', 'tab-userDetail');
                                custom_obj.list_fn.reload();
                                break;
                        }
                    } else {
                        layer.msg(data.msg, {anim: 6});
                    }
                }
            });
        }
    }
    if ($('#userDetail')[0] && !$('#userDetail').attr('hasRender')) {
        userDetail_fn.initRender();
        // 设置页面高度
        $(window).resize(function() {
            cmsUtils.setContentTabHeight('userDetailContent', 50);
        });
    }
    // 添加
    layui.form.on('submit(userDetail-btn-add)', function (data) {
        if (!userDetail_fn.verifyForm()) {
            return;
        }
        layer.confirm('确定添加该帐号吗？', {
            btn:['确定', '取消']
        }, function (index) {
            userDetail_fn.submitForm(userDetail_fn.getFormData(data.field), 'close', index);
        });
    });
    // 修改
    layui.form.on('submit(userDetail-btn-modify)', function (data) {
        if (!userDetail_fn.verifyForm()) {
            return;
        }
        var msg = '确定保存修改信息？';
        if (data.field.phone != old_phone) {
            msg = '确定修改联系方式？修改后平台短信消息都将发送至新手机号！';
        }
        layer.confirm(msg, {
            btn:['确定', '取消']
        }, function (index) {
            userDetail_fn.submitForm(userDetail_fn.getFormData(data.field), 'close', index);
        });
    });
});